package com.zhiwei.fast.activitys

import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.navigation.Navigation
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.zhiwei.fast.R
import com.zhiwei.fast.databinding.ActivityMainBinding
import com.zhiwei.fast.activitys.models.MainActivityViewModel
import com.zhiwei.most.base.activitys.BaseVmVbActivity
import com.zhiwei.most.ext.klog

class MainActivity : BaseVmVbActivity<MainActivityViewModel, ActivityMainBinding>() {


    private var mExitTime = 0L

    override fun initView(savedInstanceState: Bundle?) {

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val nav = Navigation.findNavController(this@MainActivity, R.id.nav_host_fragment)
                if (nav.currentDestination != null && nav.currentDestination!!.id != R.id.mainFragment) {
                    //如果当前界面不是主页，那么直接调用返回即可
                    nav.navigateUp()
                } else {
                    //是主页
                    if (System.currentTimeMillis() - mExitTime > 2000) {
                        ToastUtils.showShort(StringUtils.getString(R.string.press_again_to_exit_the_program))
                        mExitTime = System.currentTimeMillis()
                    } else {
                        ToastUtils.cancel()  //防止内存泄漏
                        finish()
                    }
                }
            }
        })
    }

    override fun createObserver() {
    }

}